package com.suse.service.user.controller.admin;

import com.suse.common.base.model.param.BasePageQueryParam;
import com.suse.common.config.response.R;
import com.suse.common.config.response.SimpleCode;
import com.suse.service.user.entity.User;
import com.suse.service.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static com.suse.common.config.response.SimpleCode.LOGIN_SUCCESS;
import static com.suse.common.config.response.SimpleCode.LOGOUT_SUCCESS;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-08-19 17:38
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/dev-api/admin/user")
public class AdminUserController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "查询用户分页")
    @PostMapping("/page")
    public R page(@RequestBody BasePageQueryParam pageQueryParam) {
        return userService.page(pageQueryParam);
    }

    @ApiOperation(value = "查询用户列表")
    @GetMapping("/list")
    public R list() {
        return R.success(userService.list());
    }

    @ApiOperation(value = "添加用户")
    @PutMapping
    public R save(@RequestBody User user) {
        boolean save = userService.save(user);
        if (save) {
            return R.success(SimpleCode.SAVE_SUCCESS);
        }else {
            return R.success(SimpleCode.SAVE_ERROR);
        }
    }

    @ApiOperation(value = "修改用户")
    @PostMapping
    public R update(@RequestBody User user) {
        boolean updateById = userService.updateById(user);
        if (updateById) {
            return R.success(SimpleCode.UPDATE_SUCCESS);
        }else {
            return R.success(SimpleCode.UPDATE_ERROR);
        }
    }

    @ApiOperation(value = "删除用户(逻辑删除)")
    @DeleteMapping("/{id}")
    public R remove(@PathVariable("id") String id) {
        boolean removeById = userService.removeById(id);
        if (removeById) {
            return R.success(SimpleCode.REMOVE_SUCCESS);
        }else {
            return R.error(SimpleCode.REMOVE_ERROR);
        }
    }

    @ApiOperation(value = "批量删除用户(逻辑删除)")
    @DeleteMapping
    public R remove(@RequestBody List<String> ids) {
        boolean removeById = userService.removeByIds(ids);
        if (removeById) {
            return R.success(SimpleCode.REMOVE_SUCCESS);
        }else {
            return R.error(SimpleCode.REMOVE_ERROR);
        }
    }

    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public R login() {
        return R.put("token","admin-token").success(LOGIN_SUCCESS);
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping("/info")
    public R info() {
        return R.put("name","Admin").put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif").put("roles","[admin]").success();
    }

    @ApiOperation(value = "用户注销")
    @PostMapping("/logout")
    public R logout() {
        return R.success(LOGOUT_SUCCESS);
    }


    @ApiOperation(value = "统计某一天注册用户的数量")
    @GetMapping("/daily/register-count/{date}")
    public R getDailyRegisterCount(@PathVariable("date") String date) {
        return userService.getDailyRegisterCount(date);
    }

}
